/***************************************************************************
 *   Copyright (C) 2008 by Rysiek Kr�l,,,   *
 *   rysiek@Babylon2   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "searchdialog.h"

#include <QFileDialog>

#include "keywordsdialog.h"

SearchDialog::SearchDialog(PictureManager *manager, Qt::WFlags fl)
        : QDialog(dynamic_cast<QWidget*>(manager), fl ), Ui::searchDialog(), manager(manager)
{
    setupUi(this);
    
    this->fromDateTimeEdit->setDate(QDate::currentDate());
    this->orginalDateFrom = new QDateTime(this->fromDateTimeEdit->dateTime());
    this->toDateTimeEdit->setDate(QDate::currentDate());
    this->orginalDateTo = new QDateTime(this->toDateTimeEdit->dateTime());
    
    this->searchRangeComboBox->addItem(this->manager->getItsProperty()->getPictureDir().dirName(),this->manager->getItsProperty()->getPictureDir().absolutePath());
    if(this->manager->getItsProperty()->getPictureDir().dirName() != this->manager->getCurDir()->dirName())
    this->searchRangeComboBox->addItem(this->manager->getCurDir()->dirName(),this->manager->getCurDir()->absolutePath());
    this->searchRangeComboBox->addItem(tr("Choose Directory"));
    this->searchRangeComboBox->insertSeparator(this->searchRangeComboBox->count()-1);


}

SearchDialog::~SearchDialog()
{
	delete this->orginalDateFrom;
	delete this->orginalDateTo;
}

/*$SPECIALIZATION$*/
/**
 * Sets Search range
 */
void SearchDialog::setSearchRange()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                  this->manager->getCurDir()->absolutePath(),
                  QFileDialog::ShowDirsOnly
                  | QFileDialog::DontResolveSymlinks);
    int i = this->searchRangeComboBox->findText(QDir(dir).dirName());
    if (i<0)
    {
        this->searchRangeComboBox->setCurrentIndex(0);
        this->searchRangeComboBox->insertItem(0, QDir(dir).dirName(), dir);
        this->searchRangeComboBox->setCurrentIndex(0);
    }
    else
    {
        this->searchRangeComboBox->setCurrentIndex(i);
    }
}

void SearchDialog::reject()
{
    QDialog::reject();
}

/**
 * Handle accept event, creating searchParameter;
 */
void SearchDialog::accept()
{
    this->searchParameter = new SearchParameter();
    if (this->keywordsEdit->text()!="")
    {
        QStringList keywords = this->keywordsEdit->text().split(",");
        this->searchParameter->setKeywords(new QSet<QString>(keywords.toSet()));
    }
    else
    {
        this->searchParameter->setKeywords(new QSet<QString>());
    }
    this->searchParameter->setDir(new QDir(this->searchRangeComboBox->itemData(this->searchRangeComboBox->currentIndex()).toString()));

    QMap< SearchParameter :: SearchType, QString > *parameter = new QMap< SearchParameter :: SearchType, QString >();
    if (!this->fileNameEdit->text().isEmpty())parameter->insert(SearchParameter::FileName, this->fileNameEdit->text());
    if (!this->placeEdit->text().isEmpty())parameter->insert(SearchParameter::Place, this->placeEdit->text());
    if (!this->noteEdit->text().isEmpty())parameter->insert(SearchParameter::Note, this->noteEdit->text());
    this->searchParameter->setParameters(parameter);

    QMap< SearchParameter :: DateType, QDateTime > *dateParameters = new QMap< SearchParameter :: DateType, QDateTime >();
    if (this->fromDateTimeEdit->dateTime()!=*this->orginalDateFrom)
        dateParameters->insert(SearchParameter::DateStart,this->fromDateTimeEdit->dateTime());

    this->searchParameter->setIsRecursive(this->recursiveSearchCheckBox->isChecked());

    if (this->toDateTimeEdit->dateTime()!=*this->orginalDateTo)
        dateParameters->insert(SearchParameter::DateEnd,this->toDateTimeEdit->dateTime());
    this->searchParameter->setDateParameter(dateParameters);
    QDialog::accept();
}

/**
 * Activates, when search range is changed
 * @param index - new current index
 */
void SearchDialog::searchRangeChanged(int index)
{
    if (index == this->searchRangeComboBox->count()-1&&this->isVisible())
    {
        QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                      this->manager->getCurDir()->absolutePath(),
                      QFileDialog::ShowDirsOnly
                      | QFileDialog::DontResolveSymlinks);
        if (!dir.isEmpty() || QDir(dir).dirName()!=".")
        {
            int i = this->searchRangeComboBox->findText(QDir(dir).dirName());
            if (i<0)
            {
                this->searchRangeComboBox->setCurrentIndex(0);
                this->searchRangeComboBox->insertItem(0, QDir(dir).dirName(), dir);
                this->searchRangeComboBox->setCurrentIndex(0);
            }
            else
            {
                this->searchRangeComboBox->setCurrentIndex(i);
            }
        }
    }
}

/**
 * Shows keyword select dialogue
 */
void SearchDialog::selectKeywordSlot()
{
    KeywordsDialog dialog(this->manager, true);
    if ( dialog.exec())  this->keywordsEdit->setText(dialog.getKeywordString());
}

/**
 *  Returns Search parameter
 * @return 
 */
SearchParameter* SearchDialog::getSearchParameter() const
{
    return searchParameter;
}
